package org.cuatrovientos.signin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Dani
 * Servlet implementation class Private
 */
@WebServlet("/Private")
public class Private extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Private() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Just redirect to private JSP
		HttpSession session = request.getSession();
		checkSession(request, response, session);
	}

	private void checkSession(HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		
		if (null != session.getAttribute("login")){
			System.out.println("ok, " + session.getAttribute("login"));
		this.getServletContext().getRequestDispatcher("/private.jsp")
        .forward(request, response);
		}else{
			this.getServletContext().getRequestDispatcher("/SignIn")
	        .forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Just redirect to private JSP
		HttpSession session = request.getSession();
		checkSession(request, response, session);
	}
}
